package com.bomberman.items;

import com.bomberman.environment.BombWorld;
import com.bomberman.environment.Man;
import com.bomberman.environment.Tele;
import com.bomberman.environment.Tile;
import java.awt.Color;

import info.gridworld.grid.*;

public class PBOOM extends BOOM
//see: PBomb
{
	public PBOOM(BombWorld world, Color Clr)
	{
		super(world, Clr);
	}
	
	
	public boolean move(String desc)
	{
		String asdw = "ASDWA";
		Location loc = getLocation();
		if(loc==null)
		{	
			w.add(startLoc,this);
			loc = startLoc;
		}
		Location toLoc = w.asdwLoc(desc, loc);
		toLoc = w.asdwLoc(asdw.substring(asdw.indexOf(desc)+1, asdw.indexOf(desc)+2), toLoc);
				
		Grid g = w.getGrid();
		if(g.isValid(toLoc))
			{
			moveTo(toLoc);
			return true;
			}
			
		return false;
	}
	
	//Simple extentions, just changed PBOOM to BOOM
	public void moveTo(Location toLoc)
	{
		Location loc = getLocation();
		
		Grid g = w.getGrid();
		if(emptyLoc(toLoc))
			{
			super.moveTo(toLoc);
			w.add(loc, new PBOOM(w, clr));
			return;
			}
		else
			{
				Class asdf = g.get(toLoc).getClass();
				if(asdf.getSuperclass().equals(Item.class))
				{
					super.moveTo(toLoc);
					w.add(loc, new PBOOM(w, clr));
					return;
				}
			else
				if(asdf.getSuperclass().equals(Tile.class))
				{
					Tile tile = ((Tile) w.getGrid().get(toLoc));
					if( tile.open && tile.occupied )
					{
						PBOOM boom = new PBOOM(w, clr);
						w.add(w.randLoc(), boom);
						boom.got( ((Tele)tile).a );
					}
				}
			else
				if(asdf.equals(Man.class))
				{
					PBOOM boom = new PBOOM(w, clr);
					w.add(w.randLoc(), boom);
					boom.got( (Man) g.get(toLoc) );
				}
			else if(asdf.equals(Bomb.class))
					w.Bombs.get(w.Bombs.indexOf(((Bomb) g.get(toLoc))))
						.explode();
				
				return;
			}
	}
}